package oneroom.admin.board;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oneroom.admin.oneroom.roomModel;
import oneroom.comm.IbatisLoader;

import com.ibatis.sqlmap.client.SqlMapClient;

public class boardDao {
	private IbatisLoader 	iBatis;
	private SqlMapClient 	sqlMapper;
	
	public boardDao(){
		this.sqlMapper = iBatis.setConfig();
	}
	
	/**
	 * 리스트
	 * @return
	 */
	public List getList(String searchBoard, String searchGubn, String searchValue, int selectPage, int pageSize){
		List returnValue = new ArrayList();
		if (selectPage == 0) selectPage = 1;
		if (pageSize == 0) pageSize = 10;
		int startPos = ((selectPage-1)*pageSize);
		Map<String, Object> param = new HashMap<String, Object>();
		if(searchBoard.equals("all") || searchBoard.equals("")){
			searchBoard = null;
		}
		param.put("searchGubn", searchGubn);
		param.put("searchValue", searchValue);
		param.put("startPos", startPos);
		param.put("pageSize", pageSize);
		param.put("searchBoard", searchBoard);
		
		try{
			returnValue = (List)sqlMapper.queryForList("board.boardlist",param);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}
	/**
	 * 게시물 전체 갯수
	 * @return
	 */
	public int getCount(String searchBoard, String searchGubn, String searchValue){
		int returnValue = 0;
		Map<String, Object> param = new HashMap<String, Object>();
		if(searchBoard.equals("all") || searchBoard.equals("")){
			searchBoard = null;
		}
		param.put("searchGubn", searchGubn);
		param.put("searchValue", searchValue);
		param.put("searchBoard", searchBoard);
		try{
			returnValue = ((Integer)sqlMapper.queryForObject("board.boardcount",param)).intValue();	
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}
	/**
	 * 상세보기
	 * @param data_id
	 * @return
	 */
	public boardModel getView(String data_id){
		boardModel returnValue = new boardModel();
		try{
			returnValue = (boardModel)sqlMapper.queryForObject("board.boardview", data_id);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}
	/**
	 * 이전글
	 * @param data_id
	 * @return
	 */
	public boardModel getPrev(String data_id, String searchBoard, String searchGubn, String searchValue){
		boardModel returnValue = new boardModel();
		if(searchBoard.equals("all") || searchBoard.equals("")){
			searchBoard = null;
		}
		HashMap param = new HashMap();
		param.put("searchGubn", searchGubn);
		param.put("searchValue", searchValue);
		param.put("data_id", data_id);
		param.put("searchBoard", searchBoard);
		try{
			returnValue = (boardModel)sqlMapper.queryForObject("board.boardprev", param);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}
	/**
	 * 다음글
	 * @param data_id
	 * @return
	 */
	public boardModel getNext(String data_id, String searchBoard, String searchGubn, String searchValue){
		boardModel returnValue = new boardModel();
		if(searchBoard.equals("all") || searchBoard.equals("")){
			searchBoard = null;
		}
		HashMap param = new HashMap();
		param.put("searchGubn", searchGubn);
		param.put("searchValue", searchValue);
		param.put("data_id", data_id);
		param.put("searchBoard", searchBoard);
		try{
			returnValue = (boardModel)sqlMapper.queryForObject("board.boardnext", param);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}
	/**
	 * 수정
	 * @param boardmodel
	 * @return
	 */
	public boolean getUpdate(boardModel boardmodel){
		boolean returnValue = false;
		try{
			if(sqlMapper.update("board.boardupdate", boardmodel) > 0){
				returnValue = true;
			}else{
				returnValue = false;
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}
	/**
	 * 조회수 증가
	 * @param boardmodel
	 * @return
	 */
	public void getHitcount(String data_id){
		try{
			sqlMapper.update("board.boardhitcount", data_id);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	/**
	 * 등록
	 * @param boardmodel
	 * @return
	 */
	public boolean getInsert(boardModel boardmodel){
		boolean returnValue = true;
		try{
			sqlMapper.insert("board.boardinsert", boardmodel);
		}catch (Exception e) {
			// TODO: handle exception
			returnValue = false;
		}
		return returnValue;
	}
	/**
	 * 삭제
	 * @param data_id
	 * @return
	 */
	public boolean getDelete(String data_id){
		boolean returnValue = false;
		try{
			if(sqlMapper.delete("board.boarddelete", data_id) > 0){
				returnValue = true;
			}else{
				returnValue = false;
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}
	
	/**
	 * 리스트
	 * @return
	 */
	public List getSelectList(String data_id){
		List returnValue = new ArrayList();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("data_id", data_id);
		
		try{
			returnValue = (List)sqlMapper.queryForList("board.select_list",param);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}	
	
	/**
	 * 게시물 MAX
	 * @return
	 */
	public int getBoardMax(String searchBoard){
		int returnValue = 0;
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("searchBoard", searchBoard);
		try{
			returnValue = ((Integer)sqlMapper.queryForObject("board.board_max",param)).intValue();	
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;		
	}
	/**
	 * 게시물 Deth
	 * @return
	 */
	public int getBoardDeth(String re_data_id,String  board_group ){
		int returnValue = 0;
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("re_data_id", re_data_id);
		param.put("board_group", board_group);
		try{
			returnValue = ((Integer)sqlMapper.queryForObject("board.board_deth",param)).intValue();	
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;		
	}		
	
	/**
	 * 수정
	 * @param boardmodel
	 * @return
	 */
	public void getGroupUpdate(boardModel boardmodel){
		boolean returnValue = false;
		try{
			sqlMapper.update("board.board_Gorup", boardmodel);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}	
	
	/**
	 * 리스트
	 * @return
	 */
	public List getMyList(String searchBoard, String id, int selectPage, int pageSize){
		List returnValue = new ArrayList();
		if (selectPage == 0) selectPage = 1;
		if (pageSize == 0) pageSize = 10;
		int startPos = ((selectPage-1)*pageSize);
		Map<String, Object> param = new HashMap<String, Object>();
		if(searchBoard.equals("all") || searchBoard.equals("")){
			searchBoard = null;
		}
		param.put("id", id);
		param.put("startPos", startPos);
		param.put("pageSize", pageSize);
		param.put("searchBoard", searchBoard);
		
		try{
			returnValue = (List)sqlMapper.queryForList("board.board_mylist",param);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}
	/**
	 * 게시물 전체 갯수
	 * @return
	 */
	public int getMyCount(String searchBoard, String id){
		int returnValue = 0;
		Map<String, Object> param = new HashMap<String, Object>();
		if(searchBoard.equals("all") || searchBoard.equals("")){
			searchBoard = null;
		}
		param.put("id", id);
		param.put("searchBoard", searchBoard);
		try{
			returnValue = ((Integer)sqlMapper.queryForObject("board.board_mycount",param)).intValue();	
		}catch (Exception e) {
			// TODO: handle exception
		}
		return returnValue;
	}
}
